package com.eastfair.auth.controller;

import com.eastfair.auth.service.ResourceOrganizationService;
import com.eastfair.auth.entity.ResourceOrganization;
import com.eastfair.auth.dto.ResourceOrganizationSaveDTO;
import com.eastfair.auth.dto.ResourceOrganizationUpdateDTO;
import com.eastfair.auth.dto.ResourceOrganizationPageQuery;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.eastfair.auth.vo.ResourceOrganizationVo;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author ligang
 * @date 2021-07-19
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/resourceOrganization")
@Api(value = "ResourceOrganization", tags = "")
public class ResourceOrganizationController extends SuperController<ResourceOrganizationService, Long, ResourceOrganization, ResourceOrganizationPageQuery, ResourceOrganizationSaveDTO, ResourceOrganizationUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<ResourceOrganization> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ResourceOrganization> resourceOrganizationList = list.stream().map((map) -> {
            ResourceOrganization resourceOrganization = ResourceOrganization.builder().build();
            //TODO 请在这里完成转换
            return resourceOrganization;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(resourceOrganizationList));
    }


    /**
     * 通过组织查询资源
     * @param orgIds 组织id
     * @return
     */
    @ApiOperation(value = "通过组织查询资源", notes = "通过组织查询资源")
    @GetMapping("/queryResource")
    public R<List<ResourceOrganizationVo>> queryResource(@RequestParam("orgIds") List<Long> orgIds) {
        return R.success(baseService.queryResource(orgIds));
    }

    /**
     * 通过组织查询资源id
     * @param orgIds 组织id
     * @return
     */
    @ApiOperation(value = "通过组织查询资源", notes = "通过组织查询资源")
    @GetMapping("/queryResourceId")
    public R<List<Long>> queryResourceId(@RequestParam("orgIds") List<Long> orgIds) {
        List<ResourceOrganizationVo> resourceOrganizationVoList = baseService.queryResource(orgIds);
        if(resourceOrganizationVoList!=null && !resourceOrganizationVoList.isEmpty()){
            List<Long> resourceIds = resourceOrganizationVoList.stream().map(ResourceOrganizationVo::getResourceId).collect(Collectors.toList());
            return R.success(resourceIds);
        }
        return R.success(null);
    }

}
